package com.hk.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hk.reggie.entity.Employee;

import com.hk.reggie.mapper.EmployeeMapper;
import com.hk.reggie.service.EmployeeService;
import com.hk.reggie.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public LoginVo loginService(Employee employee) {
        Employee one = this.getOne(new QueryWrapper<Employee>().eq("username", employee.getUsername()));
        LoginVo loginVo = new LoginVo();
        loginVo.setEmployee(one);
        if(one == null){
           loginVo.setInfo("用户名不存在");
        }else{
            if(!one.getPassword().equals(employee.getPassword())){
                loginVo.setInfo("密码错误");
            }else{
                if(one.getStatus() != 1){
                    loginVo.setInfo("该账户已被锁定");
                }else{
                    loginVo.setInfo("OK");
                }
            }
        }
        return loginVo;
    }

    @Override
    public void saveEmployee(Employee employee) {
        //设置初始密码，并进行MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        this.save(employee);
    }
}
